﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DownPicFromWeb
{
    class Program
    {
        static void Main(string[] args)
        {
            //为了下载动森图鉴图片而设置的
            //https://wiki.biligame.com/dongsen/%E5%AE%B6%E5%85%B7%E5%9B%BE%E9%89%B4

            string url = "";
            string re = null;
            do
            {
                Console.WriteLine("请输入网页地址");
                url = Console.ReadLine();
                re = GetWeb(url);

                ////////////////////////修改这里！
                int change = 0;
                ////////////////////////修改这里！

                if(null != re)
                {
                    List<string> picList = new List<string>();
                    List<string> nameList = new List<string>();

                    switch (change)
                    {
                        case 0://下载家具、DIY、服装类
                            {
                                picList = SubstringMultiple(re, "srcset=\"", " 2x\"");

                                int a = picList.Count;

                                Console.WriteLine("一共 {0} 个，开始下载……", picList.Count);

                                for (int i = 0; i < picList.Count; i++)
                                {
                                    Console.WriteLine("正在下载第 {0} / {1} 张图片...", i + 1, picList.Count);
                                    string[] pl = picList[i].Split(',');

                                    string name = SubstringSingle(pl[0], ".png/", " 1.5x");

                                    DownPic(pl[1], name);
                                }
                                Console.WriteLine("下载完毕");
                            }
                            break;
                        case 1://下载曲目
                            {
                                picList = SubstringMultiple(re, "\" src=\"", "\" decoding=");
                                nameList = SubstringMultiple(re, "img alt=\"", "\" src=");

                                int a = picList.Count;
                                int b = nameList.Count;

                                Console.WriteLine("一共 {0} 个，开始下载……", picList.Count);

                                for (int i = 0; i < picList.Count; i++)
                                {
                                    Console.WriteLine("正在下载第 {0} / {1} 张图片...", i + 1, picList.Count);

                                    string name = nameList[i];

                                    DownPic(picList[i], name);
                                }
                                Console.WriteLine("下载完毕");
                            }
                            break;
                        default:
                            break;
                    }
                }
            } while (null == re);
            Console.Read();
        }

        static string GetWeb(string url)
        {
            string result = null;
            try
            {
                WebClient client = new WebClient();
                result = client.DownloadString(url);
                Console.WriteLine("成功 - 读取网页");
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("失败 - 读取网页");
            }
            return result;
        }

        static bool DownPic(string url,string filename)
        {
            string path = @"Pic\" + filename;
            try
            {
                WebClient client = new WebClient();
                client.DownloadFile(url, path);
                Console.WriteLine("成功 - 下载图片");
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("失败 - 下载图片");
                return false;
            }
        }

        /// <summary>
        /// 截取字符串中开始和结束字符串中间的字符串
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>中间字符串</returns>
        static string SubstringSingle(string source, string startStr, string endStr)
        {
            Regex rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline);
            return rg.Match(source).Value;
        }

        /// <summary>
        /// （批量）截取字符串中开始和结束字符串中间的字符串
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>中间字符串</returns>
        static List<string> SubstringMultiple(string source, string startStr, string endStr)
        {
            Regex rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline);

            MatchCollection matches = rg.Matches(source);

            List<string> resList = new List<string>();

            foreach (Match item in matches)
                resList.Add(item.Value);

            return resList;
        }
    }
}
